<template><div><h2 id="数据填充" tabindex="-1"><a class="header-anchor" href="#数据填充"><span>数据填充</span></a></h2>
<p>Laravel 5.5 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-数据库之-数据填充" tabindex="-1"><a class="header-anchor" href="#laravel-数据库之-数据填充"><span>Laravel 数据库之：数据填充</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#writing-seeders">编写 Seeders</a>
<ul>
<li><a href="#using-model-factories">使用模型工厂</a></li>
<li><a href="#calling-additional-seeders">调用其他 Seeders</a></li>
</ul>
</li>
<li><a href="#running-seeders">运行 Seeders</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 可以用 seed 类轻松地为数据库填充测试数据。所有的 seed 类都存放在 <code v-pre>database/seeds</code> 目录下。你可以任意为 seed 类命名，但是更应该遵守类似 <code v-pre>UsersTableSeeder</code> 的命名规范。Laravel 默认定义的一个 <code v-pre>DatabaseSeeder</code> 类。可以在这个类中使用 <code v-pre>call</code> 方法来运行其它的 seed 类从而控制数据填充的顺序。</p>
<h2 id="编写-seeders" tabindex="-1"><a class="header-anchor" href="#编写-seeders"><span>编写 Seeders</span></a></h2>
<p>通过运行 <a href="https://learnku.com/docs/laravel/5.5/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> <code v-pre>make:seeder</code> 来生成 Seeder。由框架生成的 seeders 都将被放置在 <code v-pre>database/seeds</code> 目录下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>seeder UsersTableSeeder</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一个 seeder 类只包含一个默认方法：<code v-pre>run</code>。这个方法会在 <a href="https://learnku.com/docs/laravel/5.5/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> <code v-pre>db:seed</code> 被执行时调用。在 <code v-pre>run</code> 方法里你可以根据需要在数据库中插入数据。你也可以用 <a href="https://learnku.com/docs/laravel/5.5/queries" target="_blank" rel="noopener noreferrer">查询构造器</a> 或 <a href="https://learnku.com/docs/laravel/5.5/database-testing#writing-factories" target="_blank" rel="noopener noreferrer">Eloquent 模型工厂</a> 来手动插入数据。</p>
<blockquote>
<p>{tip} 在数据填充期间，<a href="https://learnku.com/docs/laravel/5.5/eloquent#mass-assignment" target="_blank" rel="noopener noreferrer">批量赋值保护</a> 会自动禁用。</p>
</blockquote>
<p>如下所示，在默认的 <code v-pre>DatabaseSeeder</code> 类中的 <code v-pre>run</code> 方法中添加一条数据插入语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Seeder</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DatabaseSeeder</span> <span class="token keyword">extends</span> <span class="token class-name">Seeder</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 运行数据库填充</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token function">str_random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token function">str_random</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">.</span><span class="token string single-quoted-string">'@gmail.com'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">bcrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'secret'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用模型工厂" tabindex="-1"><a class="header-anchor" href="#使用模型工厂"><span>使用模型工厂</span></a></h3>
<p>手动为每个模型填充指定属性很麻烦。作为替代方案，你可以使用 <a href="https://learnku.com/docs/laravel/5.5/database-testing#writing-factories" target="_blank" rel="noopener noreferrer">模型工厂</a> 来轻松地生成大量数据库数据。首先，阅读 <a href="https://learnku.com/docs/laravel/5.5/database-testing#writing-factories" target="_blank" rel="noopener noreferrer">模型工厂文档</a> 来学习如何使用工厂，然后就可以使用 <code v-pre>factory</code> 这个辅助函数来向数据库中插入数据。</p>
<p>例如，创建 50 个用户并为每个用户创建关联：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 运行数据库填充</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token function">factory</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">each</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$u</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$u</span><span class="token operator">-></span><span class="token function">posts</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token function">factory</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Post</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="调用其他-seeders" tabindex="-1"><a class="header-anchor" href="#调用其他-seeders"><span>调用其他 Seeders</span></a></h3>
<p>在 <code v-pre>DatabaseSeeder</code> 类中，你可以使用 <code v-pre>call</code> 方法来运行其他的 seed 类。使用 <code v-pre>call</code> 方法可以将数据填充拆分成多个文件，这样就不会使单个 seeder 变得非常大。只需简单传递要运行的 seeder 类名称即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Run the database seeds.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">UsersTableSeeder</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">PostsTableSeeder</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">CommentsTableSeeder</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="运行-seeders" tabindex="-1"><a class="header-anchor" href="#运行-seeders"><span>运行 Seeders</span></a></h2>
<p>完成 seeder 类的编写之后，你可能需要使用 <code v-pre>dump-autoload</code> 命令重新生成 Composer 的自动加载器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer dump<span class="token operator">-</span>autoload</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接着就可以使用 Artisan 命令 <code v-pre>db:seed</code> 来填充数据库了。默认情况下，<code v-pre>db:seed</code> 命令将运行 <code v-pre>DatabaseSeeder</code> 类，这个类可以用来调用其它 Seed 类。不过，你也可以使用 <code v-pre>--class</code> 选项来指定一个特定的 seeder 类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan db<span class="token punctuation">:</span>seed</span>
<span class="line"></span>
<span class="line">php artisan db<span class="token punctuation">:</span>seed <span class="token operator">--</span><span class="token keyword">class</span><span class="token operator">=</span>UsersTableSeeder</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以使用 <code v-pre>migrate:refresh</code> 命令来填充数据库，该命令会回滚并重新运行所有迁移。这个命令可以用来重建数据库：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate<span class="token punctuation">:</span>refresh <span class="token operator">--</span>seed</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/5435" target="_blank" rel="noopener noreferrer">@wqer1019</a></td>
<td><img src="https://avatars3.githubusercontent.com/u/9254545?v=4&amp;s=100" alt="9254545?v=4&amp;s=100"></td>
<td>翻译</td>
<td>laravel是世界上最优雅的框架，<a href="https://github.com/wqer1019" target="_blank" rel="noopener noreferrer">@wqer1019</a> at Github</td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


